<template>
  <el-dialog :visible.sync="show" title="分配权限" @close="closeEvent">
    <!-- el-tree
           default-checked-keys:设置默认勾选，只管勾选，不管取消
           this.$refs.tree.getCheckedKeys():获取已勾选项的值
           this.$refs.tree.setCheckedKeys([])：清空已勾选项的值
    -->
    <el-tree
      ref="tree"
      :data="list"
      show-checkbox
      check-strictly
      :props="{ label: 'name' }"
      node-key="id"
      :default-checked-keys="permIds"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button>取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleId, sysRoleAssignPrem } from '@/api/setting'
export default {
  data() {
    return {
      show: false,
      list: [],
      permIds: [],
      id: ''
    }
  },
  methods: {
    async getData() {
      if (this.list.length === 0) {
        const res = await sysPermission()
        this.list = changeData(res.data)
        //   console.log(res)
      }
    },
    // 获取角色详情
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      this.permIds = res.data.permIds
      //   this.$refs.tree.setCheckedKeys(res.data.permIds)
      //   console.log('角色详情', res)
    },
    // 确定点击事件
    async submit() {
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      // 提示
      this.$message.success('分配权限成功')
      // 关闭弹窗
      this.show = false
    },
    // 关闭回调
    closeEvent() {
      //   this.permIds = []
      this.$refs.tree.setCheckedKeys([])
    }
  }
}
</script>
<style></style>
